﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T12OOP
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 类与其构造函数
            Account acc1 = new Account();
            Console.WriteLine(acc1);
            Account acc2 = new Account("张三", 1000);
            Console.WriteLine(acc2);
            #endregion


            #region 单例模式
            SingleTon s1 = SingleTon.GetInstance();
            SingleTon s2 = SingleTon.GetInstance();
            if (s1 == s2)
            {
                Console.WriteLine("SingleTon类的两个实例s1与s2是相同的!");
            }
            #endregion

            #region 属性
            Student stu = new Student();
            stu.Name = "张三";
            //异常处理:友好的对待有可能会出现错误
            try
            {
                stu.Age = 21;
                stu.Age = -21;
            }
            catch (Exception)
            {
                Console.WriteLine("年龄不能超过指定范围");
            }

            Console.WriteLine(stu);
            #endregion

            #region 索引属性
            Contact c = new Contact("张三");
            //c.Name="只读属性不能赋值"；
            Console.WriteLine(c.Name);
            c["mobile"] = "13907921111";
            c["home"] = "0792-8311029";
            c["fax"] = "+086-0792-8311029";
            c["business"] = "0792-8311102";
            Console.WriteLine(c);
            #endregion

            #region 预定义操作符重载
            GameValue gv1 = new GameValue();
            gv1.Hp = 40;
            gv1.Mp = 30;
            gv1.Sp = 20;
            GameValue gv2 = new GameValue();
            gv2.Hp = 30;
            gv2.Mp = 30;
            gv2.Sp = 5;

            Console.WriteLine(gv1 + gv2);

            #endregion

            Console.ReadKey();
        }
    }
}


namespace T12OOP
{
    class Contact
    {
        private string name;
        private string homePhone;
        private string mobilePhone;
        private string busiPhone;
        private string fax;



        //只读属性
        public string Name
        {
            get { return name; }
        }

        //索引方式属性
        public string this[string type]
        {
            get
            {
                switch (type)
                {
                    case "mobile": return mobilePhone;
                    case "home": return homePhone;
                    case "business": return busiPhone;
                    case "fax": return fax;
                    default: return "none";
                }
            }
            set
            {
                switch (type)
                {
                    case "mobile": mobilePhone = value; break;
                    case "home": homePhone = value; break;
                    case "business": busiPhone = value; break;
                    case "fax": fax = value; break;
                }
            }
        }

        public Contact(string name)
        {
            this.name = name;
        }

        public override string ToString()
        {
            return string.Format("联系人信息:【{0}】【{1}】【{2}】【{3}】【{4}】",
                name, homePhone, mobilePhone, busiPhone, fax);
        }
    }
}

namespace T12OOP
{
    class GameValue
    {
        private int hp; //健康值
        private int mp; //魔法值
        private int sp; //精力值
        public int Hp { get { return hp; } set { hp = value; } }
        public int Mp { get { return mp; } set { mp = value; } }
        public int Sp { get { return sp; } set { sp = value; } }

        public override string ToString()
        {
            return string.Format(" 健康值:{0} , 魔法值: {1}, 精力值: {2}", hp, mp, sp);
        }

        public static GameValue operator +(GameValue gv1, GameValue gv2)
        {
            //定义了在GameValue在对象上" + "号的运算规则
            GameValue _gv = new GameValue();

            _gv.hp = gv1.hp + gv2.hp;
            _gv.mp = gv1.mp + gv2.mp;
            _gv.sp = gv1.sp + gv2.sp;

            return _gv;
        }
    }
}


namespace T12OOP
{
    /// <summary>
    ///简单单例模式类
    /// </summary>
    class SingleTon
    {
        private static SingleTon _instance;

        private SingleTon()
        {
            Console.WriteLine("创建单例对象");
        }

        public static SingleTon GetInstance()
        {
            if (_instance == null)
            {
                _instance = new SingleTon();
            }

            return _instance;
        }
    }
}


namespace T12OOP
{
    class Account
    {
        private string id;
        private decimal money;

        public Account()
        {
            this.id = "";
            this.money = 0;
        }

        /// <summary>
        /// 带参的构造方法
        /// </summary>
        /// <param name="id"></param>
        /// <param name="money"></param>

        public Account(string id, decimal money)
        {
            //显示构造函数后，默认的无参构造函数不在自由系统自动生成
            //变量的作用域，使用this关键字
            //通常构造函数中是初始化成员属性
            this.id = id;
            this.money = money;
        }

        /// <summary>
        /// 重写基类（父类）的TOString方法
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            //return base.ToString();
            return string.Format("账户信息-->账户名:{0},余额:{1}", id, money);
        }
    }
}

namespace T12OOP
{
    class Student
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int age;

        //在属性中增加数据的约束
        public int Age
        {
            get { return age; }
            set
            {
                if (value <= 0 | value > 100)
                    throw new ArgumentNullException("年龄参数异常");
                age = value;
            }
        }

        public override string ToString()
        {
            return string.Format("{0}:{1}", name, age);
        }


    }
}
